using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using Universum.Model.Services.Personals;
using Universum.Model.Services;
using Universum.Model.Domain;
using System.IO;
using Universum.Infrastructure.Configuration;
using Universum.Infrastructure.Persistence;
using Universum.Infrastructure.Persistence.Queries.Resource;

namespace Universum.Controllers
{
	[Authorize]
	public class PersonalController : UniversumController
	{
		IRepository _Repository;
		IFileService _FileService;
		IPersonalService _PersonalService;
		public PersonalController(
			System.Security.Principal.IPrincipal principal,
			IRepository repository,
			IFileService fileService,
			IPersonalService personalService
			)
			: base(principal)
		{
			_FileService = fileService;
			_Repository = repository;
			_PersonalService = personalService;
		}
		//
		// GET: /Personal/
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult UpdatePicture(HttpPostedFileBase newPicture)
		{

			var Picture = _PersonalService.SaveOrUpdateUserPicture(newPicture);
			_FileService.WriteToDiskFrom(newPicture, Picture.Path);
			return RedirectToAction("Index", "Home");
		}
		
		public ActionResult UserPicture()
		{
			
			if (_PersonalService.HasPicture.IsFalse())
			{
				return new EmptyResult();
			}
			var picture = _PersonalService.GetPicture();
			return 
				new FileStreamResult(
				_FileService.GetStreamFor(picture.Path),
				picture.ContentType);
		}

		[AcceptVerbs( HttpVerbs.Post)]
		public ActionResult UpdateDescription(string description)
		{
			_PersonalService.SaveOrUpdateUserDescription(description);
			return RedirectToAction("Index", "Home");
		}
	}
}
